local skel = fk.CreateSkill {
  name = "lb__chenli",
}

local DIY = require "packages.diy_utility.diy_utility"

Fk:loadTranslationTable{
  ["lb__chenli"] = "沉离",
  [":lb__chenli"] = "出牌阶段限一次，你可以令所有角色选择一项：1.对自己造成1点伤害；2.弃置一张手牌。因此受到伤害的角色若此后未再失去过牌，本阶段结束时其回复1点体力。",

  ["@@lb__chenli-phase"] = "沉离",

  ["#lb__chenli"] = "沉离：你可以令所有角色选择一项：1.对自己造成1点伤害；2.弃置一张手牌",
  ["#lb__chenli_dis"] = "沉离：你可以弃置一张手牌，或取消后对自己造成1点伤害",

  ["$lb__chenli1"] = "命运并非一种选择。",
  ["$lb__chenli2"] = "难以领会的预言。",
}

skel:addEffect("active", {
  prompt = "#lb__chenli",
  can_use = function (self, player)
    return player:usedSkillTimes(skel.name, Player.HistoryPhase) < 1
  end,
  card_filter = Util.FalseFunc,
  target_filter = Util.FalseFunc,
  on_use = function (self, room, skillUseEvent)
    local player = skillUseEvent.from
    local current = room.logic:getCurrentEvent()
    local phase = current:findParent(GameEvent.Phase, true)
    for _, to in ipairs(room:getAlivePlayers()) do
      if not to.dead then
        if to:isKongcheng() or #room:askToDiscard(to, {
          min_num = 1,
          max_num = 1,
          prompt = "#lb__chenli_dis",
          cancelable = true,
          include_equip = false,
          skill_name = skel.name,
        }) == 0 then
          room:setPlayerMark(to, "@@lb__chenli-phase", 1)
          if phase then
            phase:addExitFunc(function ()
              if player:hasSkill(skel.name) and not to.dead and to:isWounded() then
                local recover = true
                phase:searchEvents(GameEvent.MoveCards, 1, function (e)
                  if e.id > current.id then
                    for _, move in ipairs(e.data) do
                      if move.from == to and table.find(move.moveInfo, function (info)
                        return info.fromArea == Card.PlayerHand or info.fromArea == Card.PlayerEquip
                      end) then
                        recover = false
                        return true
                      end
                    end
                  end
                end)
                if recover then
                  room:recover{
                    who = to,
                    num = 1,
                    skillName = skel.name,
                    recoverBy = player,
                  }
                end
              end
            end)
          end
          room:damage{
            from = to,
            to = to,
            damage = 1,
            skillName = skel.name,
          }
        end
      end
    end
  end,
})
--宝宝标记
skel:addEffect(fk.AfterCardsMove, {
  can_refresh = function(self, event, target, player, data)
    if player:getMark("@@lb__chenli-phase") > 0 then
      for _, move in ipairs(data) do
        if move.from == player then
          for _, info in ipairs(move.moveInfo) do
            if info.fromArea == Card.PlayerHand or info.fromArea == Card.PlayerEquip then
              return true
            end
          end
        end
      end
    end
  end,
  on_refresh = function(self, event, target, player, data)
    player.room:setPlayerMark(player, "@@lb__chenli-phase", 0)
  end,
})

return skel
